package com.turing.post.account.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.account.util.AccountBusiness;
import com.turing.post.account.util.DepositAccountParam;
import com.turing.post.account.util.DepositAccountResult;
import com.turing.post.account.util.SalesGroup;
import com.turing.post.account.util.SubAccount;
import com.turing.post.account.util.SubAccountBusiness;
import com.turing.post.account.util.SubAccountDao;
import com.turing.post.cardmanagement.util.ActivateCard;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.cardmanagement.util.AppManagement;
import com.turing.post.cardmanagement.util.AppManagementDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "account.impl.SubAccountBusinessImpl")
@Remote
@Local
public class SubAccountBusinessImpl implements SubAccountBusiness {

	@EJB(mappedName = "cardmanagement.impl.AppManagementDaoImpl")
	private AppManagementDao appManagementDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@EJB(mappedName = "account.impl.SubAccountDaoImpl")
	private SubAccountDao subAccountDaoImpl;

	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao acDao;

	@EJB(mappedName = "account.impl.AccountBusinessImpl")
	private AccountBusiness accountBusinessImpl;

	/**
	 * 创建子帐户
	 * 
	 * @param masterAccNo
	 *            主帐号
	 * @param openBranchNo
	 *            开户机构号
	 * @param openTime
	 *            开户时间
	 * @param salesGroupId
	 *            销售员组ID
	 * @return 子帐户号
	 * @throws PCPSApplicationException
	 *             异常信息
	 */
	public BigInteger createSubAccount(BigInteger masterAccNo,
			String openBranchNo, Date openTime, BigInteger salesGroupId)
			throws PCPSApplicationException {
		// 产生子帐号
		BigInteger subAccNo = pcpsCommonDaoImpl.getUniqueIntegerKey();

		// 设置记表参数
		SubAccount subacc = new SubAccount();

		subacc.setSubAccNo(subAccNo);
		subacc.setMasterAccNo(masterAccNo);
		subacc.setOpenBranchNo(openBranchNo);
		subacc.setOpenTime(openTime);
		subacc.setLastAccountTime(openTime);
		subacc.setSalesGroupId(salesGroupId);
		subacc.setBalance(BigInteger.ZERO);

		// 记表
		subAccountDaoImpl.insert(subacc);

		return subAccNo;
	}

	/**
	 * 更新子帐户余额
	 * 
	 * @param subAccNo
	 *            子帐号
	 * @param amount
	 *            发生额
	 * @param accTime
	 *            记帐时间
	 */
	public BigInteger updateBalance(BigInteger subAccNo, BigInteger amount,
			Date accTime) throws PCPSApplicationException {

		// 以锁定方式根据子帐号检索子帐户信息
		SubAccount sac = subAccountDaoImpl
				.getSubAccountBySubAccNoForUpdate(subAccNo);
		if (sac == null)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
					TuxedoRespCode.RC_INVALID, "子帐号" + subAccNo.toString()
							+ "不存在!");

		// 设置记帐时间
		sac.setLastAccountTime(accTime);

		// 更新余额
		sac.addAmount(amount);

		// 检查余额
		if (sac.getBalance().compareTo(BigInteger.ZERO) < 0)
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION,
					TuxedoRespCode.RC_LACK, "子帐号" + subAccNo.toString()
							+ "记帐金额" + amount.toString() + "/"
							+ sac.getBalance().toString() + "非法!");

		// 更新
		subAccountDaoImpl.update(sac);

		// 返回最新余额
		return sac.getBalance();
	}

	/**
	 * 账户子系统记账
	 * 
	 * @param cardNo
	 * @param amount
	 * @param remark
	 * @param issueCompany
	 * @param tranTime
	 * @param localSeqNo
	 * @return
	 * @throws PCPSApplicationException
	 */
	public String insertSubAccountInfo(String cardNo, BigInteger amount,
			String remark, IssueCompany issueCompany, Date tranTime,
			String localSeqNo) throws PCPSApplicationException {

		ActivateCard ac = acDao.getActivateCardByCardNo(cardNo);
		ArrayList<SalesGroup> salesList = new ArrayList<SalesGroup>();
		ArrayList<DepositAccountParam> depositAccountParamList = new ArrayList<DepositAccountParam>();

		DepositAccountParam ap = new DepositAccountParam();
		ap.masterAccNo = ac.getMasterAccNo();
		ap.depositAmount = amount;
		ap.remark = remark;

		depositAccountParamList.add(ap);

		DepositAccountResult dar = accountBusinessImpl.DepositAccount(
				issueCompany.getIssueCompanyNo(), ac.getSaleBranchNo(), null,
				issueCompany.getTranDate(), tranTime, localSeqNo, null, remark,
				depositAccountParamList, salesList, null);

		// 更新卡余额&累计充值额
		ac = acDao.getActivateCardByCardNoForUpdate(ac.getCardNo());
		ac.setTotalDeposit(ac.getTotalDeposit().add(amount));
		acDao.update(ac);
		return dar.accSeqNo;
	}

	@Override
	public BigInteger createAppManageSubAccount(BigInteger masterAccNo,
			String openBranchNo, Date openTime, String appSeqNo)
			throws PCPSApplicationException {
		// 产生子帐号
		BigInteger subAccNo = pcpsCommonDaoImpl.getUniqueIntegerKey();
		// 设置记表参数
		SubAccount subacc = new SubAccount();

		subacc.setSubAccNo(subAccNo);
		subacc.setMasterAccNo(masterAccNo);
		subacc.setOpenBranchNo(openBranchNo);
		subacc.setOpenTime(openTime);
		subacc.setLastAccountTime(openTime);
		subacc.setSalesGroupId(null);// 销售员组
		subacc.setBalance(BigInteger.ZERO);
		AppManagement am = appManagementDaoImpl
				.getAppManagementByAID(AppManagement.AppManagement_Gas);
		subacc.setAppId(am.getAppId());
		subacc.setAppSeqNo(appSeqNo);
		// 记表
		subAccountDaoImpl.insert(subacc);

		return subAccNo;
	}
}
